package com.aigou.admin.dao.impl;

import com.aigou.admin.utils.JDBCUtils;
import com.aigou.admin.dao.OrderDao;
import com.aigou.admin.entity.Order;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class OrderDaoImpl implements OrderDao {

    @Override
    public List<Order> selectAll() {
        return JDBCUtils.executeQuery(Order.class, "select * from s_order");
    }

    @Override
    public Order selectById(String id) {
        List<Order> Order = JDBCUtils.executeQuery(Order.class, "select * from s_order where id = ? ",  id);
        if (Order.isEmpty()) {
            return null;
        }
        return Order.get(0);
    }

    @Override
    public Boolean insert(Order order) {
        int rows = JDBCUtils.executeUpdate("insert into s_order(id,create_time,receiving_address,user_id) values(?,?,?,?)",
                order.getId(), order.getCreate_time(), order.getReceiving_address(), order.getUser_id());
        return rows > 0;
    }

    @Override
    public Boolean update(Order order) {
        int rows = JDBCUtils.executeUpdate("update s_order set create_time=?,receiving_address=?,user_id=? where id = ?",
                order.getCreate_time(), order.getReceiving_address(), order.getUser_id(), order.getId());
        return rows > 0;
    }

    @Override
    public Boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_order where id = ?", id);
        return rows >0;
    }

    @Override
    public Integer selectOrderCount() {
        return JDBCUtils.executeCount("select count(*) from s_order");
    }

    @Override
    public List<Order> selectOrderByLimit(Integer start, Integer limit) {
        return JDBCUtils.executeQuery(Order.class, "select * from s_order limit ?,?", start, limit);
    }
}
